const { mongo, Types, Schema } = require('mongoose');
let mongoose = require('./db');

// 用户表
let userSchema = new mongoose.Schema({
  name: String,
  password: String,
  phone: Number,
  email: String,
  gender: String,
  birthday: Date,
  registerTime: { type: Date, default: Date.now },
  address: String,
  useImage: String,
  weiBoId: String,
});

let userModel = mongoose.model('user', userSchema, 'user');

//房源表
let houseSchema = new mongoose.Schema({
  houseName: String,
  houseImg: Array,
  houseAddress: String,
  houseType: String,
  housePrice: Number,
  houseCreateTime: Date,
  houseStatus: Boolean,
  houseUser: {
    type: mongoose.Types.ObjectId,
    ref: 'user',
  },
  // 去地图上请求位置去添加
  lnglat: Array,
  weight: Number,
  fans: {
    type: [mongoose.Types.ObjectId],
    ref: 'user',
  },
});

let houseModel = mongoose.model('house', houseSchema, 'house');

// 租房表
let tenantSchema = new mongoose.Schema({
  tenantTime: Date,
  tenantUser: {
    type: mongoose.Types.ObjectId,
    ref: 'user',
  },
  teaddress: String,
  // 去地图上请求位置去添加
  tenantDemand: String,
  tenantPrice: String,
  tenantType: String,
  fans: {
    type: [mongoose.Types.ObjectId],
    ref: 'user',
  },
});

let tenantModel = mongoose.model('tenant', tenantSchema, 'tenant');

// 对话表
let friendSchema = new mongoose.Schema({
  usersid: {
    type: [mongoose.Types.ObjectId],
    ref: 'user',
  },
  time: { type: Date, default: Date.now },
  content: String,
});
let friendModel = mongoose.model('friend', friendSchema, 'friend');

// 消息
const messageSchema = new mongoose.Schema({
  userid: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'user',
  },
  receiveid: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'user',
  },
  friendid: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'friend',
  },
  content: String,
  time: { type: Date, default: Date.now },
  isRead: { type: Boolean, default: false },
  type: Number,
});
const messageModel = mongoose.model('messages', messageSchema, 'message');

const codeSchema = new mongoose.Schema({
  phone: String,
  email: String,
  code: String,
  time: { type: Date, default: Date.now },
});

const codeModel = mongoose.model('code', codeSchema, 'code');

let customSchema = new mongoose.Schema({
  userId: {
    type: mongoose.Types.ObjectId,
    ref: 'user',
  },
  socketId: String,
});

let customModel = mongoose.model('custom', customSchema, 'custom');

module.exports = {
  userModel,
  houseModel,
  tenantModel,
  friendModel,
  messageModel,
  codeModel,
  customModel,
};
